import net.spreadshirt.shop.lib.ProducttypeSize;

/**
 * The ProducttypeSizes class stores all sizes of every product type.
 * It provides some methods to handle the size items.
 */
class net.spreadshirt.shop.lib.ProducttypeSizes {

	// A list of all article items.
	private var _sizes:Array;

	/**
	 * ProducttypeSizes Constructor
	 */
	public function ProducttypeSizes(items:Array) {
		// Check if items list is containing one or more items.
		if(items.length > 0) {
			addProducttypeSizes(items);
		}
	}

	/**
	 * Clears all values.
	 */
	public function clear():Void {
		_sizes = new Array();
	}

	/**
	 * Stores a list of product type size items.
	 * The old list will be overwritten.
	 * 
	 * @param	items	A list of product type size items.
	 */
	public function addProducttypeSizes(items:Array):Void {
		// Clear product type sizes list.
		clear();
		// Adds all items.
		for(var i:Number = 0; i < items.length; i++) {
			addProducttypeSize(items[i]);
		}
	}

	/**
	 * Stores a single product type size item in the sizes list.
	 * 
	 * @param	item	A single sizes item.
	 */
	public function addProducttypeSize(item:Object):Void {
		// Only adds the item if there's no other item
		// with the same size ID already stored.
		if(!isExisting(item)){
			var size:ProducttypeSize = new ProducttypeSize(item);
			_sizes.push(size);
		}
	}

	/**
	 * Returns the complete list of all sizes.
	 * 
	 * @return	A list of size items.
	 */
	public function getAllItems():Array {
		return _sizes;
	}

	/**
	 * Checks if the product type size is already stored.
	 * 
	 * @param	item	A single product type size item.
	 * 
	 * @return	true or false
	 */
	private function isExisting(item:Object):Boolean {
		var found:Boolean = false;
		for(var i:Number = 0; i < _sizes.length; i++) {
			if(item.producttype_size_id == _sizes[i].producttype_size_id) {
				found = true;
				break;
			}
		}
		return found;
	}

}
